package vn.aptech.scrumgroup.model;

import vn.aptech.scrumgroup.physic.Vector2d;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import demo.axitdn.physic.Vector3f;

public class AvatarModel extends AbstractBaseModel{

	public float scale;
	//mother circle of avatar
	public CircleWithTextModel motherCircle;
	public CircleWithTextModel centerCircle;
	public float angle;
	//with and ratio with/height rectangle bound
	public float width;
	public float ratio;
	private float height;
	//distance between circle center and left-bottom point of avatar
	public float distance; 
	private float left;
	private float bottom;
	private float top;
	private float right;
	private boolean hasMulti = false;
	
	public AvatarModel(CircleWithTextModel motherCircle, CircleWithTextModel centerCircle, float distance, float width, float ratio, boolean hasMulti, int backColor) {
		// TODO Auto-generated constructor stub
		super(backColor, -1);
		this.motherCircle = motherCircle;
		this.centerCircle = centerCircle;
		this.distance = distance;
		this.width = width;
		this.ratio = ratio;
		this.height = width*ratio;
		calculateLeftBottom();
		this.hasMulti = hasMulti;
	}//end ctor
	
	private void calculateLeftBottom() {
		// TODO Auto-generated method stub
		Vector2d connectString= new Vector2d(motherCircle.cx-centerCircle.cx, motherCircle.cy-centerCircle.cy);
		//distance of center cirlce and left-bottom point
		float d = (float)Math.sqrt((motherCircle.cx-centerCircle.cx)*(motherCircle.cx-centerCircle.cx)+(motherCircle.cy-centerCircle.cy)*(motherCircle.cy-centerCircle.cy)) + motherCircle.radius;
		//normalize vector connect between 2 center
		Vector3f direction = connectString.normalize();
		//calculate vector between center and left-point
		Vector3f vDistance = direction.mult(d+distance);
		
		left = centerCircle.cx + vDistance.x - width/2 ;
		bottom = centerCircle.cy + vDistance.y + height/2;
		right = left + width;
		top = bottom - height;
	}//end method

	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(backColor);
		calculateLeftBottom();
		if (hasMulti){
			int oldAlpha = paint.getAlpha();
			paint.setAlpha(oldAlpha/2);
			drawAvatar(c, left+width/3, top, right+width/3, bottom, paint);
			drawAvatar(c, left-width/3, top, right-width/3, bottom, paint);
			paint.setAlpha(oldAlpha);
		}//end if
		//draw main avatar
		drawAvatar(c, left, top, right, bottom, paint);
	}//end draw

	public void drawAvatar(Canvas c, float left, float top, float right, float bottom, Paint paint) {
		//draw head
		float headX = left + width/2;
		float headY = top + height/6;
		c.drawCircle(headX, headY, height/6, paint);

		//draw body
		float bodyLeft = left;
		float bodyTop = top + (0.7f/1.8f)*height;
		float bodyRight = right;
		float bodyBottom = bottom;
		
		c.drawRect(bodyLeft, bodyTop, bodyRight, bodyBottom, paint);
		//draw arm 1
		float armInsideX = left + width/6;
		float armInsideY = top + height/2;
		float space = width/20;
		float armBelowX = left + width/6 + space;
		float armBelowY = top + height;
		
		int oldColor = paint.getColor();
		paint.setColor(Color.WHITE);
		c.drawRect(armInsideX, armInsideY, armBelowX, armBelowY, paint);
		
		//draw arm 2
		armInsideX = right - width/6;
		armBelowX = right - width/6 - space;
		c.drawRect(armInsideX, armInsideY, armBelowX, armBelowY, paint);
		paint.setColor(oldColor);
	}

	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub
		
	}//end method

}
